/**
* This file is part of tictionary.
*
* tictionary is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* http://code.google.com/p/tictionary/
*/

package dicterator.dictTreeCreator;

import dicterator.Utils;
import dicterator.dict.Dict;
import dicterator.dict.Letter;
import dicterator.dict.LetterSet;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author Erol Hira
 */
public class Serializer {

    private Dict dict;

    private DataOutputStream out;

    public Serializer(Dict dict) {

        this.dict = dict;
        
        try {
            out = new DataOutputStream(new FileOutputStream(dict.getDictFile()));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public void serialize(){

        LetterSet rootSet = dict.getRootSet();

        try {
            write(rootSet);
            writeMean(rootSet);
            out.flush();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void write(LetterSet letterSet) throws Exception{

        if(letterSet == null){
            return;
        }
        
        out.writeByte(letterSet.getLetterCount());

        for(int i = 0; i < letterSet.getLetterCount(); i++){

            Letter letter = letterSet.getLetter(i);

            String chs = letter.getChars();

            if(chs != null){
            //if(letter.getChildSet() == null){

                out.writeByte(chs.length());
                for(int j = 0; j < chs.length(); j++){
                    out.writeByte(Utils.getCharAsByte(chs.charAt(j)));
                }

            } else {

                out.writeByte(Utils.getCharAsByte(letter.getLetter()));
                out.writeShort(letter.getChildSetOffset());
            }

            out.writeShort(letter.getMeanOffset());
        }
        
        for(int i = 0; i < letterSet.getLetterCount(); i++){

            Letter letter = letterSet.getLetter(i);

            LetterSet childSet = letter.getChildSet();
            write(childSet);
        }
    }

    public void writeMean(LetterSet letterSet) throws IOException{

        if(letterSet == null){
            return;
        }

        for(int i = 0; i < letterSet.getLetterCount(); i++){

            Letter letter = letterSet.getLetter(i);

            if(letter.getMean() != null){
                byte[] meanBytes = Utils.string2bytes(letter.getMean());
                out.writeByte(meanBytes.length);
                out.write(meanBytes);
            }
        }

        for(int i = 0; i < letterSet.getLetterCount(); i++){

            Letter letter = letterSet.getLetter(i);

            LetterSet childSet = letter.getChildSet();
            writeMean(childSet);
        }
    }
}
